package org.baitiaojun.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import org.baitiaojun.pojo.CheckItem;
import org.baitiaojun.service.CheckItemService;
import org.baitiaojun.util.MessageConstant;
import org.baitiaojun.vo.PageResult;
import org.baitiaojun.vo.QueryPageBean;
import org.baitiaojun.vo.Result;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;


/**
 * @类名 CheckItemController
 * @描述 TODO
 * @作者 白条君
 * @创建日期 2022/5/24 19:37
 * @版本 1.0
 */

@Controller
@RequestMapping(value = "/pages")
public class CheckItemController {

    @Reference(interfaceClass = CheckItemService.class)
    private CheckItemService checkItemService;

    @RequestMapping(value = "/saveCheckItem.do", method = RequestMethod.POST)
    public @ResponseBody Result saveCheckItem(@RequestBody(required = true) CheckItem checkItem) {
        Result result = new Result();
        try {
            int res = checkItemService.saveCheckItem(checkItem);
            if (res != 0) {
                result.setFlag(true);
                result.setMessage(MessageConstant.ADD_CHECKITEM_SUCCESS);
            } else {
                result.setFlag(false);
                result.setMessage(MessageConstant.ADD_CHECKITEM_FAIL);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setFlag(false);
            result.setMessage(MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return result;
    }

    @RequestMapping(value = "/queryPageBeanPageResult.do", method = RequestMethod.POST)
    public @ResponseBody PageResult<CheckItem> queryCheckItemBySplitPage(@RequestBody QueryPageBean queryPageBean) {
        return checkItemService.queryCheckItemBySplitPage(queryPageBean);
    }

    @PostMapping("/removeCheckItemById.do")
    public @ResponseBody Result removeCheckItemById(@RequestParam Integer id) {
        Result result = new Result();
        try {
            checkItemService.removeCheckItemById(id);
            result.setFlag(true);
            result.setMessage(MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFlag(false);
            result.setMessage(MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return result;
    }

    @GetMapping("/queryCheckItemById.do")
    public @ResponseBody Result queryCheckItemById(@RequestParam Integer id) {
        CheckItem checkItem = checkItemService.queryCheckItemById(id);
        Result result = null;
        if (!ObjectUtils.isEmpty(checkItem)) {
            result = new Result();
            result.setFlag(true);
            result.setData(checkItem);
        }
        return result;
    }

    @PostMapping("/editCheckItemById.do")
    public @ResponseBody Result editCheckItemById(@RequestBody CheckItem checkItem) {
        Result result = new Result();
        try {
            int res = checkItemService.editCheckItemById(checkItem);
            if (!ObjectUtils.isEmpty(res)) {
                result.setFlag(true);
                result.setMessage(MessageConstant.EDIT_CHECKITEM_SUCCESS);
            } else {
                result.setFlag(false);
                result.setMessage(MessageConstant.EDIT_CHECKITEM_FAIL);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setFlag(false);
            result.setMessage(MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return result;
    }
}